//
//  AddressInfoInputCell.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/26.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "AddressInfoInputCell.h"
#import "LXTextField.h"

@interface AddressInfoInputCell ()<UITextFieldDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *myTextField;
@end

@implementation AddressInfoInputCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews{
    UILabel *titleLabel = [UILabel initWithTextColor:[UIColor blackColor] fontStr:UIPFThinFont(15)];
    [self.contentView addSubview:titleLabel];
    [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.left.equalTo(self.contentView.mas_left).offset(kAppWidth_Mini(40));
        make.width.mas_equalTo(kAppWidth_Mini(200));
    }];
    self.titleLabel = titleLabel;
    
    LXTextField *myTextField = [LXTextField new];
    myTextField.textColor = [UIColor blackColor];
    myTextField.font = UIPFThinFont(15);
    myTextField.placeholder = @" ";
    myTextField.lxPlaceHolderFont = UIPFThinFont(15);
    myTextField.lxPlaceHolderColor = [UIColor lightGrayColor];
    myTextField.textAlignment = NSTextAlignmentRight;
    [myTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.contentView addSubview:myTextField];
    [myTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kAppWidth_6(85));;
        make.right.mas_equalTo(-kAppWidth_6(25));
        make.height.mas_equalTo(kAppWidth_6(30));
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    self.myTextField = myTextField;
}

-(void)textFieldDidChange:(UITextField *)textField{
    if (_dataModel.controlLength > 0) {
        [LXTextField ControlTextLengthWith:textField length:_dataModel.controlLength];
    }
    [_dataModel.dataModel setValue:textField.text forKey:_dataModel.key];
}

-(void)setDataModel:(EditAddressModel *)dataModel{
    _dataModel = dataModel;
    self.titleLabel.text = _dataModel.title;
    self.myTextField.text = [_dataModel.dataModel valueForKey:_dataModel.key];
    self.myTextField.placeholder = _dataModel.placeHolder;
    
    if(_dataModel.keyboardType == KeyBoardTypeNumber){
        self.myTextField.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        self.myTextField.keyboardType = UIKeyboardTypeDefault;
    }
}

@end
